package ifi.p17.model;

import ifi.p17.Constant;
import ifi.p17.Person;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.table.AbstractTableModel;

public class DataModel extends AbstractTableModel implements Observer{
    //private static final int MAX = 8;
    private static final String[] names = {Constant.NAME, Constant.HOME_PHONE, Constant.WORK_PHONE, Constant.HOME_ADDRESS, Constant.WORK_ADDRESS};
    private List<Person> persons = new ArrayList<Person>();
    public void setData(List<Person> persons){
    	this.persons = persons;
    }
    public void insertPerson(Person person){
    	this.persons.add(person);
    	fireTableDataChanged();
    }
    @Override
    public int getRowCount() {
        return persons.size();
    }

    @Override
    public int getColumnCount() {
        return names.length;
    }

    @Override
    public Object getValueAt(int row, int col) {
    	Person person = (Person) persons.get(row);
    	switch (col) {
		case 0:
			return person.getName();
		case 1:
			return person.getHomeTelephone();
		case 2:
			return person.getWorkTelephone();
		case 3:
			return person.getHomeAddress();
		case 4:
			return person.getHomeTelephone();
			
		default:
			return "";
		} 
    }

    @Override
    public void setValueAt(Object aValue, int row, int col) {
        if (col == 1) {
            //persons.get(row).set= (Boolean) aValue;
            this.fireTableCellUpdated(row, col);
            
        }
    }

    @Override
    public Class<?> getColumnClass(int col) {
    	 return getValueAt(0,col).getClass();
    }

    @Override
    public String getColumnName(int col) {
        return names[col];
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        return col == 1;
    }
	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		
	}
    
}
